import cv2
import numpy as np
import os
from config import Config

detector = cv2.wechat_qrcode_WeChatQRCode(os.path.join(Config.QR_HOME, 'detect.prototxt'),
                                          os.path.join(Config.QR_HOME, 'detect.caffemodel'),
                                          os.path.join(Config.QR_HOME, 'sr.prototxt'),
                                          os.path.join(Config.QR_HOME, 'sr.caffemodel'))


def qr_decode(image_path: str) -> list:
    """
    调用微信二维码解码
    :param image_path: 图片路径
    :return: 结果
    """
    img = cv2.imread(image_path)
    res, points = detector.detectAndDecode(img)
    points = np.array(points).tolist()
    result = []
    for i, element in enumerate(res):
        result.append({"text": element, "location": points[i]})
    return result
